package com.coin.exchange.util;

import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class HttpUtil {

	public static String httpGet(String url, Map<String, Object> params) throws Exception {
		RestTemplate restTemplate = new RestTemplate();
		if (params != null && !params.isEmpty()) {
			url += "?";
			for (String key : params.keySet()) {
				url += key + "=" + params.get(key) + "&";
			}
			url = url.substring(0, url.length() - 1);
		}
		String result = restTemplate.getForObject(url, String.class);
		return result;
	}
	
	public static String httpPostFormData(String url, Map<String, Object> params) throws Exception {
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
		MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
		for (String key : params.keySet()) {
			map.add(key, params.get(key).toString());
		}
		HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(map, headers);
		String result = restTemplate.postForObject(url, requestEntity, String.class);
		return result;
	}
	
}
